package ms.shop.controller;

import com.alibaba.fastjson.JSONObject;
import ms.common.exception.BizException;
import ms.common.page.Page;
import ms.common.util.string.StringUtil;
import ms.shop.bean.ClassifyParamBean;
import ms.shop.bean.ClassifyQueryParamBean;
import ms.shop.config.ResponseCode;
import ms.shop.entity.Classify;
import ms.shop.service.ClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Project Name: ms
 * File Name: ClassifyController
 * Package Name: ms.shop.controller
 * Date: 2018/5/31  10:53
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@RestController
@RequestMapping("/classify")
public class ClassifyController {
    @Autowired
    private ClassifyService classifyService;

    /**
     * 分页查询
     * @param classifyQueryParamBean
     * @return
     */
    @PostMapping("/query")
    public Page<Classify> query(@RequestBody ClassifyQueryParamBean classifyQueryParamBean) {
        if(StringUtil.isEmpty(classifyQueryParamBean.getPageNo())) {
            classifyQueryParamBean.setPageNo(1);
        }
        if(StringUtil.isEmpty(classifyQueryParamBean.getPageSize())) {
            classifyQueryParamBean.setPageSize(10);
        }
        if(StringUtil.isEmpty(classifyQueryParamBean.getName())) {
            classifyQueryParamBean.setName("");
        }
        if(StringUtil.isEmpty(classifyQueryParamBean.getPid())) {
            classifyQueryParamBean.setPid(0L);
        }
        if(StringUtil.isEmpty(classifyQueryParamBean.getStatus())) {
            classifyQueryParamBean.setStatus(0);
        }

        String key = JSONObject.toJSONString(classifyQueryParamBean.getName()+((0 == classifyQueryParamBean.getStatus())?"":"1")+((0 == classifyQueryParamBean.getPid())?"":"1"));
        if("\"\"".equals(key)) {
            key = "";
        }
        Page<Classify> page = classifyService.query(classifyQueryParamBean, key);
        return page;
    }

    /**
     * 分类列表
     * @param classifyQueryParamBean
     * @return
     */
    @PostMapping("/list")
    public List<Classify> list(@RequestBody ClassifyQueryParamBean classifyQueryParamBean) {
        if(StringUtil.isEmpty(classifyQueryParamBean.getPid())) {
            classifyQueryParamBean.setPid(0L);
        }
        if(StringUtil.isEmpty(classifyQueryParamBean.getStatus())) {
            classifyQueryParamBean.setStatus(0);
        }

        return classifyService.list(classifyQueryParamBean, "");
    }

    /**
     * 新增
     * @param classifyParamBean
     * @return
     */
    @PostMapping("/create")
    public void create(@RequestBody ClassifyParamBean classifyParamBean) {
        //参数校验
        checkParam(classifyParamBean);

        classifyService.create(classifyParamBean);
    }

    /**
     * 修改
     * @param classifyParamBean
     * @return
     */
    @PostMapping("/update")
    public void update(@RequestBody ClassifyParamBean classifyParamBean) {
        //参数校验
        checkParam(classifyParamBean);

        classifyService.update(classifyParamBean);
    }


    /**
     * 启用
     * @param id
     * @return
     */
    @PostMapping("/use/{id}")
    public void use(@PathVariable("id") Long id) {
        classifyService.use(id);
    }

    /**
     * 停用
     * @param id
     * @return
     */
    @PostMapping("/cancel/{id}")
    public void cancel(@PathVariable("id") Long id) {
        classifyService.cancel(id);
    }
    /**
     * 详情
     * @param id
     * @return
     */
    @PostMapping("/detail/{id}")
    public Classify detail(@PathVariable("id") Long id) {
        return classifyService.detail(id);
    }

    /**
     * 清除缓存
     * @return
     */
    @PostMapping("/clear")
    public void clear() {
        classifyService.clear();
    }

    /**
     * 校验新增和修改参数
     * @param classifyParamBean
     */
    private void checkParam(ClassifyParamBean classifyParamBean) {
        if(StringUtil.isEmpty(classifyParamBean.getName())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "分类名称不能为空");
        }
        if(StringUtil.isEmpty(classifyParamBean.getImg())) {
            classifyParamBean.setImg("");
        }
        if(StringUtil.isEmpty(classifyParamBean.getPid())) {
            classifyParamBean.setPid(0L);
        }
        if(StringUtil.isEmpty(classifyParamBean.getStatus())) {
            classifyParamBean.setStatus(2);
        }
    }
}
